package com.b2b2c.store.single.logic;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.b2b2c.store.single.entity.StoreSeller;
import com.b2b2c.store.single.mapper.StoreSellerMapper;
import com.b2b2c.store.single.service.ILoginService;
import com.b2b2c.until.MD5;

public class StoreSellerLogic extends AbstractLogic implements ILoginService<StoreSeller>{


	public StoreSellerLogic(Map<String, String> args) {
		super(args);
		// TODO Auto-generated constructor stub
	}
	
	public StoreSellerLogic(HttpServletRequest req) {
		super(req);
		// TODO Auto-generated constructor stub
	}
	
	public StoreSeller loginBySeller()
	{
		
		String sellerName = this.getReq().getParameter("seller_name");
		
		StoreSeller seller = this.getSqlTemp().getMapper(StoreSellerMapper.class).login(sellerName);
		  
		return seller;
	}
	
	
	
	/**
	 * 
	 */
	public Map<String, Map<String, String>> getValidateRule() {
		// TODO Auto-generated method stub
		Map<String, Map<String, String>> rule = new HashMap<String, Map<String,String>>();
		
		Map<String, String> sellerName = new HashMap<String, String>();
		
		sellerName.put("required", "请输入卖家用户名");
		
		sellerName.put("checkUserName", "用户名只能是 中英文及其中线的组合");
		
		rule.put("seller_name", sellerName);
		
		
		Map<String, String> password = new HashMap<String, String>();
		
		password.put("required", "请输入密码");
		
		rule.put("password", password);
		
		return rule;
	}

	public Map<String, String> checkLogin() {
		// TODO Auto-generated method stub
		
		StoreSeller seller = this.loginBySeller();
		
		if (seller == null) {
			return null;
		}
		
		String pwd = seller.getPassword();
		
		String comparePassword = MD5.MD5Encode(this.getReq().getParameter("password"));
		
		if (!pwd .equals(comparePassword)) {
			return null;
		}
		
		HttpSession session = this.getReq().getSession();
		
		session.setAttribute("store_id", seller.getStoreId());
		
		session.setAttribute("id", seller.getId());
		
		Map<String, String> res = new HashMap<String, String>();
		
		res.put("token", session.getId());
		
		return res;
	}

}
